package sysman;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import common.serialization.IWebText;
import common.service.BaseService;
import common.service.CacheService;
import common.service.IService;
import common.service.ServiceFactory;
import common.web.action.BaseAction;

public class ConfigAction {
	public static IWebText getAllTable() throws Exception {
		ConfigService service = new ConfigService();
		return service.getAllTable();
	}
	
	//获取所有数据,用于不分页的列表页面
	public static List<Map<String,Object>> getListData(HttpServletRequest request) throws Exception {
		//获取service的代理类
		IConfigService service = (IConfigService) ServiceFactory.getService(new ConfigService());
				
		//从url中传过来
		String tableName = request.getParameter("tableName");
		request.setAttribute("tableName", tableName);
		
		//获取表的配置信息
		//List<Map<String,Object>> configList = service.queryBySql("select * from SYSMAN_TABLE_CONFIG where TABLE_NAME = '" + tableName + "' order by SHOW_ORDER ");
		List<Map<String,Object>> configList = CacheService.getDataByCacheId("SYSMAN_TABLE_CONFIG", "TABLE_NAME", tableName);
		BaseAction.setDD(request, null, configList);
		request.setAttribute("configList", configList);
		request.setAttribute("pkName", BaseAction.getPkName(configList));//主键字段名
		request.setAttribute("buttonMap", CacheService.getDataById("SYSMAN_DD", "CODE", tableName + "_BUTTON"));
		
		//组装页面的搜索条件
		String condSql = BaseAction.getCondition(request, configList);
		String pid = request.getParameter("pid");
		if(StringUtils.isNotEmpty(pid)) {
			condSql += " and pid='" + pid + "' order by TREE_ORDER ";
			request.setAttribute("pid", pid);
		} else {
			String pidName = request.getParameter("pidName");
			pid = request.getParameter(pidName);
			if(StringUtils.isNotEmpty(pid)) {
				condSql += " and " + pidName + "='" + pid + "' ";
				request.setAttribute(pidName, pid);
				
				String orderName = request.getParameter("orderName");
				if(StringUtils.isNotEmpty(orderName)) {
					condSql += " order by " + orderName;
				}
			}
		}
		
		//查询表的所有数据
		List<Map<String,Object>> pageList = service.queryBySql("select * from " + tableName + " where 1=1 " + condSql);
		if(!"0".equals(pid)) {//初始化配置信息
			service.initTableConfig(pid, pageList);
		}
		
		//翻译数据
		BaseAction.parseDD(pageList, configList);
		
		request.setAttribute("pageList", pageList);
		
		return pageList;
	}
	
	//根据ID获取数据,用于编辑页面
	public static Map<String,Object> getEntityData(HttpServletRequest request) throws Exception {
		//获取service的代理类
		IService service = ServiceFactory.getService(null);

		//从url中传过来
		String tableName = request.getParameter("tableName");
		request.setAttribute("tableName", tableName);
		
		//获取表的配置信息
		//List<Map<String,Object>> configList = service.queryBySql("select * from SYSMAN_TABLE_CONFIG where TABLE_NAME = '" + tableName + "' order by SHOW_ORDER ");
		List<Map<String,Object>> configList = CacheService.getDataByCacheId("SYSMAN_TABLE_CONFIG", "TABLE_NAME", tableName);
		request.setAttribute("configList", configList);
		
		//根据ID获取数据
		Map<String,Object> map = new HashMap<String,Object>();
		String ids = request.getParameter("id");
		if(StringUtils.isNotEmpty(ids)) {
			String[] idArr = ids.split(",");
			String id1 = idArr[0];
			String id2 = idArr[1];
			map = service.queryById("select * from " + tableName + " where TABLE_NAME='" + id1 + "' and FIELD_NAME='" + id2 + "'" );
		}
		request.setAttribute("entity", map);
		
		//设置数据字典
		BaseAction.setDD(request, map, configList);
		
		return map;
	}
	
	//保存
	public static void doSave(HttpServletRequest request) throws Exception {
		String tableName = request.getParameter("tableName");
		
		boolean isInsert = false;
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		List<Map<String,Object>> configList = CacheService.getDataByCacheId("SYSMAN_TABLE_CONFIG", "TABLE_NAME", tableName);
		for(Map<String,Object> configMap : configList) {
			String fieldName = (String)configMap.get("FIELD_NAME");//字段名
			String fieldValue = request.getParameter(fieldName);
			Integer isPk = (Integer)configMap.get("IS_PK");//是否主键
			String fieldType = (String)configMap.get("FIELD_TYPE");
			if(1 == isPk) {
				sql2.append(" and " + fieldName + "='" + fieldValue + "'");
			} else {
				//修改
				sql1.append("," + fieldName + "=" + BaseAction.getSqlByType(fieldType, fieldName, fieldValue, isInsert));
			}
		}
	
		String sql = "update " + tableName + " set " + sql1.toString().substring(1) + " where 1=1 " + sql2.toString();
		
		//IService service = ServiceFactory.getService(null);
		IService service = new BaseService();
		service.executeBySql(sql);
	}
}
